<?php

/**
 * @copyright Copyright 2012 CrApache-GUI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 */

include 'edit_settings.php';

function get_loaded_modules(){

    $conf_file = get_conf_original_location();
    $handle = fopen($conf_file, "r");

    $loaded_modules = array();

    while(!feof($handle)){

        $line = trim(fgets($handle));
        if(preg_match('/^LoadModule/', $line) == 1){
            $tmp = preg_split('/\s/', $line, 2);
            $loaded_modules[] = trim($tmp[1]);
        }
    }

    fclose($handle);

    return $loaded_modules;
}

function get_un_loaded_modules(){

    $conf_file = get_conf_original_location();
    $handle = fopen($conf_file, "r");

    $loaded_modules = array();

    while(!feof($handle)){

        $line = trim(fgets($handle));
        if(preg_match('/^#LoadModule/', $line) == 1){
            $tmp = preg_split('/\s/', $line, 2);
            $loaded_modules[] = trim($tmp[1]);
        }
    }

    fclose($handle);

    return $loaded_modules;
}

function is_loaded($module){

    $loaded_modules = get_loaded_modules();

    foreach($loaded_modules as $mod){
        if($mod == $module)
            return true;
    }
    return false;
}

function add_modulle($module){

    if(is_loaded($module))
        return;

    $conf_file = get_conf_original_location();
    $handle = fopen($conf_file, "r+");

    $file = "";

    while(!feof($handle)){
        $line = trim(fgets($handle));
        preg_replace('/^#LoadModule '."$module".'/', "LoadModule $module", $line);
        $file .= $line;
    }

    fwrite($handle, $file);

    fclose($handle);
}

function remove_module($module){

    $conf_file = get_conf_original_location();
    $handle = fopen($conf_file, "r+");

    $file = "";

    while(!feof($handle)){
        $line = trim(fgets($handle));
        preg_replace('/^LoadModule '."$module".'/', "#LoadModule $module", $line);
        $file .= $line;
    }

    fwrite($handle, $file);

    fclose($handle);
}

?>